﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Reflection;

namespace ProDataUpdater
{
    public static class DBHelper
    {
        public static T ConvertToEntity<T>(IDataReader iReader) where T : class,new()
        {
            T t = new T();
            Type type = t.GetType();
            PropertyInfo[] propertys = type.GetProperties();
            foreach (PropertyInfo p in propertys)
            {
                var name = p.Name;
                var value = iReader[name];
                if (value != null && value != DBNull.Value)
                {
                    p.SetValue(t, value, null);
                }
            }
            return t;
        }

        public static T ConvertToEntity<T>(DataRow iReader) where T : class,new()
        {
            T t = new T();
            Type type = t.GetType();
            PropertyInfo[] propertys = type.GetProperties();
            foreach (PropertyInfo p in propertys)
            {
                var name = p.Name;
                var value = iReader[name];
                if (value != null && value != DBNull.Value)
                {
                    p.SetValue(t, value, null);
                }
            }
            return t;
        }

    }
}
